package org.airline.impl;

import java.util.List;

import org.airline.dao.INoticeDao;
import org.airline.entity.Notice;
import org.airline.entity.PageBean;
import org.springframework.stereotype.Component;

@Component("NoticeDaoImpl")
@SuppressWarnings("all")
public class NoticeDaoImpl extends HibernateDaoImpl implements INoticeDao {

	// 删除公告对象
	@Override
	public void deleteNotice(int id) throws Exception {
		super.delete(Notice.class, id);
	}

	// 获取所有公告
	@Override
	public List<Notice> getAllNotice() throws Exception {
		return (List<Notice>)super.find("FROM Notice");
	}

	// 根据发布时间查询前5条记录
	@Override
	public List<Notice> getNoticeByDate() throws Exception {
		String hql = "FROM Notice ORDER BY createDate DESC"; 
		return (List<Notice>)super.findPageByQuery(hql, 0, 5);
	}

	// 分页查询
	@Override
	public PageBean queryForPage(int pageSize, int page) throws Exception {
		final String hql = "FROM Notice ORDER BY id";
		return super.queryForPage(hql, pageSize, page);
	}

	// 持久化公告信息
	@Override
	public void saveNotice(Notice notice) throws Exception {
		super.save(notice);
	}

	// 更新公告信息
	@Override
	public void updateNotice(Notice notice) throws Exception {
		super.update(notice);
	}

	// 根据ID查询公告
	@Override
	public Notice getNoticeById(int id) throws Exception {
		return (Notice)super.get(Notice.class, id);
	}

	//很据关键字分页查询
	@Override
	public PageBean queryForPageByKeyWords(String keyWord, int pageSize,
			int page) throws Exception {
		String hql = "FROM Notice "
			+ "WHERE title LIKE '%" + keyWord + "%' "
			+ "OR content LIKE '%" + keyWord + "%' "
			+ "OR createDate LIKE '%" + keyWord + "%' "
			+ "OR user.userName like '%" + keyWord + "%' "
			+ "OR user.trueName LIKE '%" + keyWord + "%' "
			+ "OR user.tel LIKE '%" + keyWord + "%' "
			+ "OR user.sex LIKE '%" + keyWord + "%' "
			+ "OR user.email LIKE '%" + keyWord + "%' "
			+ "OR user.address LIKE '%" + keyWord + "%' ";
	return super.queryForPage(hql, pageSize, page);
	}
	
}
